package com.epam.Parsers;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.epam.Xml.Hotel;
import com.epam.Xml.TouristVoucher;

public class SAXParserEpam {

	private static Logger log = Logger.getLogger(SAXParserEpam.class);
	private static InputStream file = SAXParserEpam.class.getClassLoader().getResourceAsStream("NewFile.xml");
	private static List<TouristVoucher> touristVouchers = new LinkedList<TouristVoucher>();

	public List<TouristVoucher> parseXML() {

		DefaultHandler dh = new DefaultHandler() {

			boolean bCost = false;
			boolean bCountry = false;
			boolean bDuration = false;
			boolean bTransport = false;
			boolean bType = false;
			boolean bHotel = false;
			boolean bName = false;
			boolean bStars = false;
			boolean bCapacity = false;
			boolean bTv = false;
			boolean bConditioning = false;
			boolean bFeeding = false;

			TouristVoucher voucher;
			Hotel hotel;

			public void startElement(String uri, String localName,
					String qName, Attributes attributes) {

				if (qName.equalsIgnoreCase("voucher")) {
					String id = attributes.getValue("id");
					voucher = new TouristVoucher();
					voucher.setId(Integer.parseInt(id));
				}

				if (qName.equalsIgnoreCase("cost")) {
					bCost = true;
				}
				if (qName.equalsIgnoreCase("country")) {
					bCountry = true;
				}
				if (qName.equalsIgnoreCase("duration")) {
					bDuration = true;
				}
				if (qName.equalsIgnoreCase("hotel")) {
					bHotel = true;
					hotel = new Hotel();
				}
				if (qName.equalsIgnoreCase("transport")) {
					bTransport = true;
				}
				if (qName.equalsIgnoreCase("Type")) {
					bType = true;
				}
				if (qName.equalsIgnoreCase("name")) {
					bName = true;
				}
				if (qName.equalsIgnoreCase("Stars")) {
					bStars = true;
				}
				if (qName.equalsIgnoreCase("Capacity")) {
					bCapacity = true;
				}
				if (qName.equalsIgnoreCase("Tv")) {
					bTv = true;
				}
				if (qName.equalsIgnoreCase("Conditioning")) {
					bConditioning = true;
				}
				if (qName.equalsIgnoreCase("Feeding")) {
					bFeeding = true;
				}
			}

			public void endElement(String uri, String localName, String qName) {
				if (qName.equalsIgnoreCase("voucher")) {
					touristVouchers.add(voucher);
				}
				if (qName.equalsIgnoreCase("hotel")) {
					voucher.setHotel(hotel);
					bHotel = false;
				}
			}

			public void characters(char[] ch, int start, int length) {

				if (bCost) {
					voucher.setCost(Double.parseDouble(new String(ch, start,
							length)));
					bCost = false;
				}
				if (bCountry) {
					voucher.setCountry(Enum.valueOf(
							TouristVoucher.Country.class, new String(ch, start,
									length)));
					bCountry = false;
				}
				if (bDuration) {
					voucher.setDuration((Integer.parseInt(new String(ch, start,
							length))));
					bDuration = false;
				}
				if (bTransport) {
					voucher.setTransport(Enum.valueOf(
							TouristVoucher.Transport.class, new String(ch,
									start, length)));
					bTransport = false;
				}
				if (bType) {
					voucher.setType(Enum.valueOf(TouristVoucher.Type.class,
							new String(ch, start, length)));
					bType = false;
				}
				if (bName) {
					hotel.setName(new String(ch, start, length));
					bName = false;
				}
				if (bStars) {
					hotel.setStars(Integer.parseInt(new String(ch, start,
							length)));
					bStars = false;
				}
				if (bCapacity) {
					hotel.setCapacity(Integer.parseInt(new String(ch, start,
							length)));
					bCapacity = false;
				}
				if (bTv) {
					hotel.setTv(Boolean.parseBoolean(new String(ch, start,
							length)));
					bTv = false;
				}
				if (bConditioning) {
					hotel.setConditioning(Boolean.parseBoolean(new String(ch,
							start, length)));
					bConditioning = false;
				}
				if (bFeeding) {
					hotel.setFeeding(Enum.valueOf(Hotel.Feeding.class,
							new String(ch, start, length)));
					bFeeding = false;
				}
			}

		};
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			sp.parse(file, dh);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return touristVouchers;
	}

	public static List<TouristVoucher> getTv() {
		return touristVouchers;
	}

	public static void setTv(TouristVoucher t) {
		SAXParserEpam.touristVouchers.add(t);
	}

}
